package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.*;
import com.itheima.service.RoleService;
import com.itheima.utils.StringNameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;


@RestController
@RequestMapping("/role")
public class RoleController {
    @Reference
    private RoleService roleService;

    //分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = roleService.pagequery(
                queryPageBean.getCurrentPage(),
                queryPageBean.getPageSize(),
                queryPageBean.getQueryString()
        );
        return pageResult;

    }

    @RequestMapping("/findAll")
    public Result findAll() {
        try {
            List<Role> roleList = roleService.findAll();
            return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS, roleList);
        } catch (Exception e) {
            e.printStackTrace();
            //服务调用失败
            return new Result(false, MessageConstant.QUERY_ROLE_FAIL);
        }
    }


    @RequestMapping("/findMenusAll")
    public Result findMenusAll() {

        List<Menu> menus = roleService.findMenusAll();

        if (menus != null && menus.size() > 0) {
            Result result = new Result(true, MessageConstant.GET_MENU_PARENT_SUCCESS);

            result.setData(menus);
            return result;
        }
        return new Result(false, MessageConstant.GET_MENU_PARENT_FAIL);
    }

    @RequestMapping("/findPermissionById")
    public Result findById(Integer id) {
        try {
            List<Integer> permission = roleService.findPermissionById(id);
            return new Result(true, MessageConstant.QUERY_PERMISSION_SUCCESS, permission);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_PERMISSION_FAIL);
        }
    }

    @RequestMapping("/add")
    public Result add(@RequestBody Role role, Integer[] permissionIds, Integer[] menuIds) {
        String regx = "[`~!@#$^&*()=|{}':;,\\[\\].<>《》/?！￥…（）—【】‘；：”“。，、？]";
        try {
            if (role.getName() != null && !"".equals(role.getName()) && !role.getName().contains(" ") && !"".matches(regx))
                roleService.add(role, permissionIds, menuIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_ROLE_FAIL);
        }
        return new Result(true, MessageConstant.ADD_ROLE_SUCCESS);
    }

    @RequestMapping("/delete")
    public Result delete(Integer id) {
        try {
            roleService.deleteById(id);
        } catch (RuntimeException r) {
            return new Result(false, r.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_ROLE_FAIL);
        }
        return new Result(true, MessageConstant.DELETE_ROLE_SUCCESS);
    }


    @GetMapping("/findMenuByRoleId")
    public Result findMenuByRoleId(Integer roleId) {
        try {
            List<Integer> menuByRoleId = roleService.findMenuByRoleId(roleId);
            List<String> menuIds = new ArrayList<>();
            for (Integer integer : menuByRoleId) {
                menuIds.add(String.valueOf(integer));
            }
            return new Result(true, MessageConstant.GET_ROLE_MENU_SUCCESS, menuIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_ROLE_MENU_FAIL);
        }
    }


    @GetMapping("/findRolePermissionByRoleId")
    public Result findRolePermissionByRoleId(Integer roleId) {
        try {
            List<Integer> result = roleService.findRolePermissionByRoleId(roleId);
            if (result != null) {
                return new Result(true, MessageConstant.GET_ROLE_PERMISSION_SUCCESS, result);
            } else {
                return new Result(false, MessageConstant.GET_ROLE_PERMISSION_FAIL);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_ROLE_PERMISSION_FAIL);
        }
    }

    @GetMapping("/findAllPermissions")
    public Result findAllPermissions() {
        try {
            List<Permission> permissionList = roleService.findAllPermissions();
            if (permissionList != null && permissionList.size() > 0) {
                return new Result(true, MessageConstant.GET_ALL_PERMISSION_SUCCESS, permissionList);
            } else {
                return new Result(false, MessageConstant.GET_ALL_PERMISSION_FAIL);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_ALL_PERMISSION_FAIL);
        }
    }


    @PostMapping("/edit")
    public Result edit(@RequestBody Role role, Integer[] permissionIds, Integer[] menuIds) {
        try {
            if (StringNameUtils.checkName(role.getName())) {
                boolean edit = roleService.edit(role, permissionIds, menuIds);
                if (edit) {
                    return new Result(edit, MessageConstant.UPDATE_ROLE_SUCCESS);
                } else {
                    return new Result(edit, MessageConstant.UPDATE_ROLE_FAIL);
                }
            }
            return new Result(false, MessageConstant.UPDATE_ROLE_FAIL);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.UPDATE_ROLE_FAIL);
        }
    }
}
